﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DAO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class UControl_uc_location : System.Web.UI.UserControl
{
    BUS_WRAREA _bus_wrarea = new BUS_WRAREA();
    BUS_WRREGION _bus_wrregion = new BUS_WRREGION();
    BUS_WRCITY _bus_wrcity = new BUS_WRCITY();

    protected void Page_Load(object sender, EventArgs e)
    {
        iSelect.ServerClick += new EventHandler(iSelect_ServerClick);
        int IDLangs = 2;
        if (!IsPostBack)
        {
            if (Session["SelLang"] != null)
            {
                if (Session["SelLang"].ToString() == "vi")
                {
                    IDLangs = 2;
                }
                else
                {
                    IDLangs = 1;
                }
            }
            else
            {
                IDLangs = 1;
            }

            BulletedList_Area.DataValueField = "ID";
            BulletedList_Area.DataTextField = "AreaName";
            BulletedList_Area.DataSource = _bus_wrarea._GetList(p => p.IDLang == IDLangs)
                .Select(p => new { p.ID, AreaName = p.AreaName + " (" + _bus_wrregion._GetList(q => q.IDArea == p.ID).ToList().Count + ")" }).ToList();
            BulletedList_Area.DataBind();
            //BulletedList_Area.Items[0].Selected = true;
        }
    }

    void iSelect_ServerClick(object sender, EventArgs e)
    {
        if (Convert.ToInt32(Session["Area"]) > 0)
            Response.Redirect("~/Page/home.html");
    }

    protected void BulletedList_Area_Click(object sender, BulletedListEventArgs e)
    {
        try
        {
            int ID = Convert.ToInt32(BulletedList_Area.Items[e.Index].Value);

            Session["Area"] = ID;

            IList<WRREGION> iListWRREGION = new List<WRREGION>();
            iListWRREGION = _bus_wrregion._GetList(p => p.IDArea == ID);

            BulletedList_Region.DataValueField = "ID";
            BulletedList_Region.DataTextField = "RegionName";
            BulletedList_Region.DataSource = iListWRREGION
                .Select(p => new { p.ID, RegionName = p.RegionName + " (" + _bus_wrcity._GetList(q => q.IDRegion == p.ID).ToList().Count + ")" }).ToList();
            BulletedList_Region.DataBind();



            BulletedList_City.DataSource = null;
            BulletedList_City.DataBind();

            BulletedList_City.Items.Clear();
            string chuoi_city = "";
            DataTable dts = Common.Common.ConvertToDataTable(iListWRREGION.ToList());
            for (int j = 0; j < dts.Rows.Count; j++)
            {
                if (j == 0)
                {
                    chuoi_city = "0";
                }
                int IDs = Convert.ToInt32(dts.Rows[j]["ID"].ToString());

                //Session["Region"] = IDs;

                IList<WRCITY> iListWRWRCITY = new List<WRCITY>();
                iListWRWRCITY = _bus_wrcity._GetList(p => p.IDRegion == IDs);

                int icount = iListWRWRCITY.Count;
                DataTable dt = Common.Common.ConvertToDataTable(iListWRWRCITY.ToList());


                
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    
                    chuoi_city += "," + dt.Rows[i]["ID"].ToString();
                    //if (j == dts.Rows.Count - 1)
                    //{
                    //    chuoi_city += "";
                    //}
                    
                        
                    
                }
                
            }
            Session["ListCity"] = chuoi_city;
            Session["Region"] = -1;
            Session["City"] = -1;
        }
        catch (Exception ex)
        {

            throw;
        }
    }
    protected void BulletedList_Region_Click(object sender, BulletedListEventArgs e)
    {
        try
        {
            int ID = Convert.ToInt32(BulletedList_Region.Items[e.Index].Value);

            Session["Region"] = ID;

            IList<WRCITY> iListWRWRCITY = new List<WRCITY>();
            iListWRWRCITY = _bus_wrcity._GetList(p => p.IDRegion == ID);

            BulletedList_City.DataValueField = "ID";
            BulletedList_City.DataTextField = "CityName";
            BulletedList_City.DataSource = iListWRWRCITY;
            BulletedList_City.DataBind();

            int icount = iListWRWRCITY.Count;
            DataTable dt = Common.Common.ConvertToDataTable(iListWRWRCITY.ToList());

            string chuoi_city = "";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i == dt.Rows.Count - 1)
                {
                    chuoi_city += dt.Rows[i]["ID"].ToString();
                }
                else
                {
                    chuoi_city += dt.Rows[i]["ID"].ToString() + ",";
                }
            }
            Session["ListCity"] = "";    
            Session["ListCity"] = chuoi_city;
                Session["City"] = -1;
        }
        catch (Exception ex)
        {

            throw;
        }
    }
    protected void BulletedList_City_Click(object sender, BulletedListEventArgs e)
    {
        try
        {
            int ID = Convert.ToInt32(BulletedList_City.Items[e.Index].Value);
            Session["CityName"] = BulletedList_City.Items[e.Index].Text.Contains('(') == true
                ? BulletedList_City.Items[e.Index].Text.Substring(0, BulletedList_City.Items[e.Index].Text.IndexOf('('))
                : BulletedList_City.Items[e.Index].Text;
            Session["City"] = ID;
            Session["ListCity"] = "";
            //Response.Redirect("~/1/Page/ViewEntry");
            //Response.Redirect("~/Pages/Default.aspx");
            Response.Redirect("~/Page/home.html");
        }
        catch
        {

            throw;
        }
    }

    




}